﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class DocTypeCls
    {
        public string DocTypeId;
        public string DocTypeName;
        public int    SortIndex=0;
        public string frkGroupDataId;
        public int TotalItem = 0;
    }

    public class DocTypeParser
    {
        public static DocTypeCls CreateInstance()
        {
            //tao 1 class trang....
            DocTypeCls oDocTypeCls = new DocTypeCls();
            return oDocTypeCls;
        }

        public static DocTypeCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            DocTypeCls oDocTypeCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oDocTypeCls;
        }


        public static DocTypeCls ParseFromDataRow(DataRow dr)
        {
            DocTypeCls oDocTypeCls = new DocTypeCls();
            oDocTypeCls.DocTypeId = XmlUtility.GetString(dr, "DocTypeId", true);
            oDocTypeCls.frkGroupDataId = XmlUtility.GetString(dr, "frkGroupDataId", true);
            oDocTypeCls.DocTypeName = XmlUtility.GetString(dr, "DocTypeName", true);
            oDocTypeCls.SortIndex = XmlUtility.GetInt(dr, "SortIndex", true);
            oDocTypeCls.TotalItem = XmlUtility.GetInt(dr, "TotalItem", true);
            return oDocTypeCls;
        }

        public static DocTypeCls[] ParseFromDataTable(DataTable dtTable)
        {
            DocTypeCls[] DocTypes = new DocTypeCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                DocTypes[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return DocTypes;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");

            ds.Tables["Info"].Columns.Add("DocTypeId");
            ds.Tables["Info"].Columns.Add("frkGroupDataId");
            ds.Tables["Info"].Columns.Add("DocTypeName");
            ds.Tables["Info"].Columns.Add("SortIndex");
            ds.Tables["Info"].Columns.Add("TotalItem");
            return ds;
        }

        public static string GetXml(DocTypeCls oDocTypeCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oDocTypeCls.DocTypeId,
                oDocTypeCls.frkGroupDataId,
                oDocTypeCls.DocTypeName,
                oDocTypeCls.SortIndex,
                oDocTypeCls.TotalItem,
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            DocTypeCls oDocType = DocTypeParser.ParseFromDataRow(dr);
            string Xml = DocTypeParser.GetXml(oDocType);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            DocTypeCls[] DocTypes = new DocTypeCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                DocTypes[iIndex] = DocTypeParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = DocTypeParser.GetXml(DocTypes);
            return Xml;
        }


        public static string GetXml(DocTypeCls[] DocTypes)
        {
            if (DocTypes == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                DocTypes = new DocTypeCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < DocTypes.Length; iIndex++)
            {
                DocTypeCls oDocTypeCls = DocTypes[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oDocTypeCls.DocTypeId,
                    oDocTypeCls.frkGroupDataId,
                    oDocTypeCls.DocTypeName,
                    oDocTypeCls.SortIndex,
                    oDocTypeCls.TotalItem,
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static DocTypeCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new DocTypeCls[0];
            }
            DocTypeCls[] DocTypes = new DocTypeCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                DocTypes[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return DocTypes;
        }
    }
}
